{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">数据库还原</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
        <script type="text/html" id="barTool">
            <a href='{:url("del")}?time={{ d.time }}' class="layui-btn layui-btn-danger layui-btn-xs layui-tr-del {:$auth->check('database/del')?'':'layui-hide'}"><i class='iconfont icon-trash_fill'></i></a>
            <a class="layui-btn layui-btn-xs import {:$auth->check('database/import')?'':'layui-hide'}" lay-event="import"><i class="iconfont icon-undo"></i> 还原</a>
            <a href='{:url("download")}?time={{ d.time }}' class="layui-btn layui-btn-xs {:$auth->check('database/download')?'':'layui-hide'}" title="下载"><i class="iconfont icon-flashlight_fill"></i> 下载</a>
        </script>
    </div>
</div>
{/block}
{block name="script"}
<script>
layui.use(['yznTable', 'form','table'], function() {
    var yznTable = layui.yznTable,
        table = layui.table,
        $ = layui.$,
        form = layui.form,
        layer = layui.layer;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTable',
    };
    
    yznTable.render({
        init: init,
        toolbar: ['refresh',[
            {
               html: "<a class='layui-btn layui-btn-sm layui-btn-danger' href='{:url('index')}'><i class='iconfont icon-other'></i> 数据库备份</a>"
            }
        ]],
        url: '{:url("restore")}',
        cols: [
            [
                { field: 'title', width: 250, title: '备份名称' },
                { field: 'date', width: 250, title: '备份时间' },
                { field: 'size', width: 120, title: '备份大小' },
                { field: 'part', width: 200, title: '卷数' },
                { field: 'compress', title: '压缩' },
                { field: '', width: 120, title: '状态', templet: '<div>-</div>' },
                { fixed: 'right', width: 180, title: '操作', toolbar: '#barTool' }
            ]
        ],
    });

    yznTable.bindevent();

    //监听行工具事件
    table.on('tool(currentTable_LayFilter)', function(obj) {
        var data = obj.data,code = ".";

        var self = this,
            url = '{:url("import")}' + '?time=' + data.time;
        layer.confirm('确定导入此条数据库吗？', { icon: 3, title: '提示' }, function(index) {
            layer.close(index);
            $.get(url, success, "json");
            window.onbeforeunload = function() { return "正在还原数据库，请不要关闭！" }
            return false;
        });

        function success(res) {
            if (res.code) {
                if (res.data.gz) {
                    res.msg += code;
                    if (code.length === 5) {
                        code = ".";
                    } else {
                        code += ".";
                    }
                }
                $(self).parents('tr').find('td:eq(5)').find('div').text(res.msg);
                //$(self).parent().prev().text(res.info);
                if (res.data.part) {
                    $.get(url, { "part": res.data.part, "start": res.data.start },
                        success,
                        "json"
                    );
                } else {
                    window.onbeforeunload = function() { return null; }
                }
            } else {
                $(self).parents('tr').find('td:eq(5)').find('div').text(res.msg);
            }
        }

    });

});
</script>
{/block}